<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>系统菜单管理页面</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">
	<meta http-equiv="X-UA-Compatible" content="IE=edge" />
	<jsp:include page="../resource.jsp"></jsp:include>
<style type="text/css">
   #editor{
     background:#E7EEF8  url(../images/main_bottom_bg.jpg) bottom right no-repeat;
   }
   .catalogTree{
     width : 450px;
   }
</style>
<script type="text/javascript">
$(function(){
	  $('#editor').omDialog({
		  autoOpen: false,
		  height: 320,
		  width : 480
	  });
	  //添加
	  $('#add').click(function(){
		  var treeNode = $("#catalogTree").omTree("getSelected");
		  var isLeaf = false;
		  if(treeNode != null) {
			  $.ajax({
				  url: "catalog!findCatalog.do",
				  data : {catalogId : treeNode.id},
				  dataType : 'json',
				  async : false,  //此处设置为同步操作
				  success: function(data){
					  isLeaf = data[0].CIsleaf;
				  }
				});
		      if(isLeaf) {
		    	  $.omMessageBox.alert({content:'叶子节点不能再添加子节点！'});
		    	  return;
		      }
		      if(treeNode.id.length/3 > 3){
		    	  $.omMessageBox.alert({content:'已经到达最大层数，不能新增！'});
		    	  return;
		      }
		  }
		  $('#catalogForm').resetForm(); //重置表单
		  $('#CId').val('');
		  $('#pText').parent().parent().show();
          if($('#root').hasClass('selected')){
        	  $('#editor').omDialog('open');
    		  $('#pCId').val('000');
    		  $('#pText').val('根节点');
          }else{
			  $('#editor').omDialog('open');
			  $('#pCId').val(treeNode.id);
			  $('#pText').val(treeNode.text);
	      }
	  });
	  
	  $('#root').click(function(){
		  $(this).parent().addClass('selected');
		  $('#catalogTree').find('span.selected').removeClass('selected');
	  });
	  
	  //修改
	  $('#modify').click(function(){
		  var treeNode = $("#catalogTree").omTree("getSelected");
		  if(treeNode == undefined){
        	  $.omMessageBox.alert({content:'请您选择修改的节点'});
        	  return ;
          }
		  $('#editor').omDialog('open');
		  $.ajax({
			  url: "catalog!findCatalog.do",
			  data : {catalogId : treeNode.id},
			  dataType : 'json',
			  success: function(data){
				  $('#pText').parent().parent().hide();
				  $('#CEnName').val(data[0].enName);
				  $('#CId').val(data[0].id);
				  $('#CCnName').val(data[0].cnName);
				  $('#CLinkUrl').val(data[0].linkUrl );
		          $('#CMenuImg').val(data[0].menuImg );
				  $('#CIsleaf').val(data[0].isleaf?1:0);
				  $('#CSort').val(data[0].sort );
			  }
			});
	  });
	  
	  //删除
	  $('#delete').click(function(){
		  var treeNode = $("#catalogTree").omTree("getSelected");
		  if(treeNode == undefined){
        	  $.omMessageBox.alert({content:'请您选择删除的节点'});
        	  return ;
          }
		  $.omMessageBox.confirm({
	           title:'确认删除',
	           content:'删除菜单后将影响系统所有用户，请确认！',
	           onClose:function(v){
	               if(v){
	            	   $(this).omAjaxSubmit({
		         			  url : 'catalog!deleteCatalog.do',
		         			  data : {catalogId : treeNode.id},
		         			  success : function(data){
		         				  if(data == '1'){ 
		         					  $.omMessageTip.show({timeout : 2000,content : '删除成功！'});
		         					  $('#catalogTree').omTree('remove',treeNode);
		         				  }else{
		         					  $.omMessageTip.show({timeout : 2000,content : '删除失败！',type : 'error'});
		         				  }
		         			  }
		         		  });
	               }
	           }
	       });
	  });
	  
	  $('#catalogTree').omTree({
          dataSource : 'catalog!queryList.do',
          widht : 350,
          onClick : function(){
        	  $('#root').parent().removeClass('selected');
          }
      });
	  $('#subform').omButton({width : 100});
	  
	  var options  = {
		  url : 'catalog!saveCatalog.do',
          success : function(data){
             	if(data != '0'){  //TODO 刷新到指定的节点，目前是整个树刷新
             		//var target = $('#catalogTree').omTree('getParent',$('#catalogTree').omTree('getSelected'));
                 	if($('#CId').val() > 0){ //修改
              			 $('#catalogTree').omTree('refresh');
                 	}else{ //新增
                 		$('#catalogTree').omTree('refresh');
                 	}
                 	$('#catalogForm').omAjaxSubmit({resetForm: true});
                 	$('#editor').omDialog('close');
                 	$.omMessageTip.show({timeout : 2000,content : '保存成功！'});
             	}else{
             		$.omMessageTip.show({timeout : 2000,content : '保存失败！',type : 'error'});
             	}
          }
	  };
	  $('#catalogForm').submit(function() {
          $(this).omAjaxSubmit(options);
          return false;//返回false,阻止浏览器默认行为
      });
});
</script>
  </head>
  <body>
    <div class="right clear" >
		<div class="toolbar mar">
		         <a href="#" class="rbutton" id="add"><span><img src="../images/add.gif" width="14" height="14" /><ins>添加</ins></span></a>
		         <A  class="line"></A>
		         <a href="#" class="rbutton" id="modify"><span><img src="../images/add.gif" width="14" height="14" /><ins>修改</ins></span></a>
		         <A  class="line"></A>
		         <a href="#" class="rbutton" id="delete"><span><img src="../images/del.gif" width="14" height="14" /><ins>删除</ins></span></a>
		</div>
		<div class="catalogTree">
		  <ul class="om-tree om-widget">
		     <li class="om-tree-node collapsable">
		        <div class="hitarea om-tree-node-hitarea -hitarea collapsable-hitarea"></div>
		        <span class="folder selected"><a href="#"  id="root">根节点</a></span>
		        <ul id="catalogTree"></ul>
		     </li>
		  </ul>
		</div>
   </div>
   
   <div id="editor" title="编辑菜单" style="display: none;">
     <form id="catalogForm" method="post">
          <table width="100%" border="0" class="grid_layout" cellspacing="0">
             <tr style="display: none;">
               <td width="10%" align="right"><span class="color_red">*</span>父节点：</td>
               <td width="28%" colspan="3">
                 <input type="text" name="pCId" id="pCId" style="display: none;"/>
                 <input type="text" name="CId" id="CId" style="display: none;"/>
                 <input type="text" name="pText" id="pText" class="input_text" readonly="readonly"/>
               </td>
             </tr>
             <tr>
               <td width="10%" align="right">英文名称：</td>
               <td width="28%" colspan="3"><input type="text" id="CEnName" name="CEnName" class="input_text" /></td>
             </tr>
             <tr>
               <td align="right">中文名称：</td>
               <td colspan="3"><input type="text" name="CCnName" id="CCnName" class="input_text"  /></td>
             </tr>
             <tr>
               <td align="right">链接地址：</td>
               <td colspan="3"><input type="text" name="CLinkUrl" id="CLinkUrl"  class="input_text"  /></td>
             </tr>
             <tr>
               <td align="right">icon图片：</td>
               <td colspan="3"><input type="text"  class="input_text" name="CMenuImg" id="CMenuImg"  /></td>
             </tr>
             <tr>
               <td align="right">是否叶子节点：</td>
               <td colspan="3">
               <select name="CIsleaf" id="CIsleaf" class="input_slelct">
                  <option value="1">是</option>
                  <option value="0">否</option>
               </select>
             </tr>
             <tr>
               <td align="right">排序：</td>
               <td colspan="3"><input type="text" name="CSort" id="CSort" class="input_text" /></td>
             </tr>
             <tr><td colspan="4" align="center"><button id="subform" type="submit"> 保&nbsp;&nbsp;&nbsp;&nbsp;存 </button></td></tr>
           </table>
         </form>
   </div>
  </body>
</html>
